<template>
  <!--  委外-->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" class="demo-form-inline">
        <el-row>
          <el-col :span="4">
            <el-form-item label="生产计划号">
              <el-input v-model="searchParam.planNo" placeholder="请输入生产计划号" clearable style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="计划状态">
              <el-select v-model="searchParam.status" placeholder="请选择计划状态" clearable style="width: 200px;">
                <el-option v-for="item in statusData" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="计划属性">
              <el-select v-model="searchParam.property" placeholder="请选择计划属性" clearable style="width: 200px">
                <el-option label="返工" :value="1" />
                <el-option label="正常" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="4">-->
<!--            <el-form-item label="设备">-->
<!--              <el-select v-model="searchParam.equipmentId" placeholder="请选择设备" clearable filterable remote-->
<!--                reserve-keyword :remote-method="remoteEquipment" style="width: 200px">-->
<!--                <el-option v-for="item in equipmentOptions" :key="item.id" :label="item.equipmentName"-->
<!--                  :value="item.id" />-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="4">
            <el-form-item label="产出产品">
              <el-input placeholder="输入产出产品编码" v-model="searchParam.productCode" style="width: 200px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
              <el-button :icon="Refresh" @click="resetSearch()">重置</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计划时间">
              <el-date-picker v-model="searchParam.times" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss"
                range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 460px;" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息 </div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" height="calc(100vh - 370px)">
        <el-table-column type="expand" fixed>
          <template #default="props">
            <div m="4">
              <el-table ref="tableCil" border size="small" :data="props.row.children"
                header-cell-class-name="blueTableHeader" style="width: 100%;margin-left: 20px">
                <el-table-column label="序号" type="index" width="50" align="center" />
                <el-table-column prop="no" label="工序订单号" align="center" min-width="150" />
                <el-table-column prop="procedureName" label="工序名称" align="center" min-width="150" />
                <el-table-column prop="productCode" label="产品编码" align="center" min-width="150">
                  <template #default="scopes"> {{ scopes.row.materialNo }} </template>
                </el-table-column>
                <el-table-column prop="materialNo" label="产出物料编码" align="center" min-width="150" />
                <el-table-column prop="orderCount" label="计划数量" width="150" align="center" />
                <el-table-column prop="reportWorkRecordCount" width="150" label="投料数量" align="center" />
                <el-table-column prop="reportCount" width="150" label="报工数量" align="center" />
                <el-table-column prop="finish" width="250" label="完成度百分比" align="center">
                  <template #default="scopes">
                    <el-progress :text-inside="false" :stroke-width="16" :percentage="scopes.row.finish" />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="序号" align="center" width="70" fixed>
          <template #default="scope">
            <!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{ (pageSearch.current - 1) * pageSearch.pageSize +
              scope.$index + 1 }} </template>
        </el-table-column>
        <el-table-column prop="planNo" label="生产计划号" align="center" min-width="200" fixed />
        <el-table-column label="计划状态" prop="status" width="150" show-overflow-tooltip align="center">
          <template #default="scope">
            <span>{{ statusData.find(i => i.value === scope.row.status)?.label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划属性" prop="property" align="center" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.property == 1">返工</span>
            <span v-else-if="scope.row.property == 0">正常</span>
          </template>
        </el-table-column>
        <el-table-column prop="productionRate" label="生产工序进度" align="center" min-width="250" />
        <el-table-column prop="reportCount" label="生产数量进度" width="250" align="center" />
        <el-table-column prop="productCode" width="250" label="产品编码" align="center" />
        <el-table-column prop="planCount" label="计划数量" width="250" align="center" />
        <el-table-column prop="processName" width="250" label="工艺路线名称" align="center" />
        <el-table-column prop="planBeginTime" width="250" label="计划开始时间" align="center" />
        <el-table-column prop="planEndTime" width="250" label="计划结束时间" align="center" />
        <!-- <el-table-column prop="status" label="单据状态" align="center" min-width="200">
          <template #default="scope">
            <span> {{ getQualityTypeByValue(scope.row.receiptsStatus) }} </span>
          </template>
        </el-table-column> -->
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="pageTotal"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';

onMounted(() => {
  getList();
  getOption();
});


const pageSearch = ref({
  current: 1,
  pageSize: 10
});


const workOrderList = ref({});
const pageTotal = ref(null);//页面总数
const searchParam = ref({});//页面检索条件
const paginatedData = ref([]);
const wareList = ref([]);//仓库
const procedureOrderList = ref([]);//工序订单
const supplierList = ref([]);
const storageAllList = ref([]);

const statusData = ref([
  { label: '新建', value: 0 },
  { label: '执行中', value: 1 },
  { label: '完成', value: 2 }
]);

const equipmentOptions = ref([])

const remoteEquipment = (value) => {
  if (!value) {
    return
  }
  axiosInstance.post('/api/basedataEquipment/getPage', {
    current: 1,
    pageSize: 50,
    equipmentName: value
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      equipmentOptions.value = res.data.records;
    });
}

const getOption = () => {

  axiosInstance.post('/api/basedataEquipment/getPage', {
    current: 1,
    pageSize: 50
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      equipmentOptions.value = res.data.records;
    });
  // axiosInstance.post('/api/outsourcingPlan/querySourcingPlanProcedureOrder', {})
  //   .then(res => {
  //     workOrderList.value = res.data;
  //   });

  // // 查询所有库位
  // axiosInstance.post('/api/basedataStorage/getAll', {})
  //   .then(res => {
  //     storageAllList.value = res.data;
  //   });


  // axiosInstance.post(`/api/basedataSupplier/getPage`, {
  //   current: 1,
  //   pageSize: 20
  // }).then(res => {
  //   if (res.code == 200) {
  //     supplierList.value = res.data.records;
  //   }
  // });


  // axiosInstance.post('/api/planProcedureOrder/getAll', {})
  //   .then(res => {
  //     if (res.code !== 200) {
  //       ElMessage.error(res.message);
  //       return false;
  //     }
  //     const list = res.data;
  //     const loop = [];
  //     for (const item of list) {
  //       loop.push(
  //         {
  //           value: item.id,
  //           label: item.materialName
  //         }
  //       );
  //     }
  //     procedureOrderList.value = list;
  //   });

  // axiosInstance.get('/api/basedataWarehouse/treeList', {}).then(res => {
  //   if (res.code !== 200) {
  //     ElMessage.error(res.message);
  //     return false;
  //   }
  //   wareList.value = res.data;
  // });
};



// 获取列表接口
const getList = () => {
  axiosInstance.post('/api/planOrder/planOrderReport', {
    ...searchParam.value,
    ...pageSearch.value,
    startDate: searchParam.value?.times?.[0],
    endDate: searchParam.value?.times?.[1],
    times: undefined,
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      // const list = res.data.records;
      // for (const item of list) {
      //   item.children = item.procedureOrderReportVo
      // }
      paginatedData.value = res.data.records;
      pageTotal.value = res.data.total;
    });
};

const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

// 重置搜索条件
const resetSearch = () => {
  searchParam.value = {};
  pageSearch.value.current = 1;
  // current.value = 1
  getList();
};

</script>
<style scoped></style>
